import EventEmitter from 'events';
export class TextModel extends EventEmitter {
  spacers_ = ''
  constructor() {
    super();
  }
  /**
   * @description 通知数据变化  
   */
  textChangeEmit_(): void{
    this.emit('text-change')
  }
  /**
   * 
   * @returns 返回当前的字符串
   */
  getSpacers(): string {
    return this.spacers_;
  }
  /**
   * @description 插入数据
   * @param spacerIndex 光标位置
   * @param spacers 插入的字符
   */
  insert(spacerIndex: number , spacers: string): void{
    const oldSpacers = this.spacers_
    this.spacers_ = oldSpacers.slice(0, spacerIndex) + spacers + oldSpacers.slice(spacerIndex)
    this.textChangeEmit_()
  }

  remove(startIndex: number , endIndex: number ): void{
    const oldSpacers = this.spacers_
   if( startIndex > endIndex ){
     [startIndex, endIndex] = [endIndex, startIndex]
   }
    this.spacers_ = oldSpacers.slice(0, startIndex) + oldSpacers.slice(endIndex)
    this.textChangeEmit_()
  }
}
export default TextModel